<?php

namespace app\api\controller;

use app\api\model\Account as Account1;
use think\Filesystem;
class Account
{

    public $AccountData;

    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token = input('get.token');
        if (empty($token)||$token == '-1') output_error('token为空');
        $this->AccountData = Account1::where('member_token','=',$token)->find();
        if (empty($this->AccountData)) output_error('token无效，请重新登录');
    }

    //注册功能
    public function getData()
    {
        //输出信息，去除“member_password”字段
        unset($this->AccountData['member_password']);
        unset($this->AccountData['member_token']);
        output_data($this->AccountData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $name = input('get.name');
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        //学生头像文件唯一，更新时需要删除原有的文件。
        $file = request()->file('image');
        $name = Filesystem::disk('avatar')->putFile('',$file,'uniqid');
        if ($this->AccountData['member_avatar'] !='default.png') {
            $filename = app()->getRootPath() . 'public\\upload\\avatar\\' . $this->AccountData['member_avatar'];
            unlink($filename);
        }
        Account1::where('member_id','=',$this->AccountData['member_id'])
            ->update(array('member_avatar'=>$name));
        output_data($name);
    }

}
